﻿using System;
using System.Collections.Generic;
using ThoughtWorks.Selenium.Silvernium;

namespace Quail.Silverlight
{
    /// <summary>
    /// The Silverlight container object (the OBJECT tag that contains the Silverlight control)
    /// </summary>
    public class SlvContainer
    {
        private readonly Dictionary<string, SlvPage> _pages = new Dictionary<string, SlvPage>();
        private bool _maximized;


        /// <summary>
        /// Initializes a new instance of the <see cref="SlvContainer"/> class.
        /// </summary>
        /// <param name="page">The web page.</param>
        /// <param name="slvObjectName">The silverlight object name.</param>
        /// <exception cref="Exception">
        /// </exception>
        public SlvContainer(WebControlContainer page, string slvObjectName)
        {
            if (!(page is WebPage))
                throw new Exception("SlvContainer page parameter must be a WebPage");

            Page = (WebPage) page;
            SlvObjectName = slvObjectName;
            SilverObject = new Silvernium(Page.Browser.SeleniumInterface, slvObjectName);
        }

        /// <summary>
        /// Gets the Silvernium object
        /// </summary>
		public Silvernium SilverObject
        {
            get; private set;
        }

        /// <summary>
        /// Gets the containing web page
        /// </summary>
        public WebPage Page
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the name of the silverlight object
        /// </summary>
        public string SlvObjectName
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the current WebBrowser instance
        /// </summary>
        public WebBrowser Browser
        {
            get { return Page.Browser; }
        }

        /// <summary>
        /// Gets the current task
        /// </summary>
        public Task ActiveTask
        {
            get { return Page.ActiveTask; }
        }

        /// <summary>
        /// Gets a new silverlight page (or view) by name
        /// </summary>
        /// <param name="slvPageName">the name of the silverlight page</param>
        /// <returns>a new silverlight page</returns>
        public SlvPage GetSlvPage(string slvPageName)
        {
            if (!_pages.ContainsKey(slvPageName))
                _pages[slvPageName] = new SlvPage(this, slvPageName);
            return _pages[slvPageName];
        }

        /// <summary>
        /// Maximizes the browser window to ensure the silverlight control is loaded
        /// </summary>
        public void Maximize()
        {
            if (_maximized)
                return;

            _maximized = true;
            try
            {
                Browser.SeleniumInterface.WindowFocus();

                // maximize the window so the silverlight control is "visible" -- if it isn't visible, it won't load!
                Browser.SeleniumInterface.WindowMaximize();

                _maximized = true;
            }
            catch (Exception)
            {
            }
        }
    }
}
